import sys
import os
import gradio as gr

# sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from utils import ArgumentParser, ConfigLoader, LOG
from model import GLMModel, OpenAIModel
from translator import PDFTranslator


 
def translate_pdf2(model_type, openai_model, book, file_format, target_language):
    args = {
        'model_type': model_type if model_type else "OpenAIModel",
        'openai_model': openai_model if openai_model else "gpt-3.5-turbo",
        'openai_api_key': os.getenv('OPENAI_API_KEY'),
        'book': book,
        'file_format': file_format if file_format else "Markdown",
        'target_language': target_language if target_language else "中文"
        }


    model_name = args['openai_model']    
    api_key = args['openai_api_key'] 
    
    model = OpenAIModel(model=model_name, api_key=api_key)

    pdf_file_path = args["book"] 
    file_format = args['file_format'] 
    target_language =args['target_language'] 

    # 实例化 PDFTranslator 类，并调用 translate_pdf() 方法
    translator = PDFTranslator(model)
    translator.translate_pdf(pdf_file_path, file_format, target_language)

    return f"翻译完成,文件存储在{os.path.dirname(book)}"


translator_dialog = gr.Interface(
    fn=translate_pdf2,
    inputs=[gr.Textbox(placeholder="请输入模型类型，默认OpenAIModel"), 
            gr.Textbox(placeholder="请输入要调用的LLM模型，默认使用GPT-3.5-turbo"), 
            gr.Textbox(placeholder="请输入要翻译PDF文件的绝对路径"), 
            gr.Textbox(placeholder="请输入翻译结果的文件件格式，PDF或Markdown,默认Markdown"), 
            gr.Textbox(placeholder="请输入翻译的目标语言，默认中文"), 
            ],
    outputs="textbox",
)
if __name__ == "__main__":
    translator_dialog.launch()  